var db = require('../../db/db');
var publictool = require("../../publictool/publictool");

exports.port = function (req, res) {
    var requestData = '';
    req.addListener('data', function (postDataChunk) {
        requestData += postDataChunk;
    });

    req.addListener('end', function () {
        publictool.putlog(req, requestData);
        var phone = publictool.checkoutdata(res, requestData);
        if (!phone) {
            return;
        }

        var client = db.client();
        db.checkuser(client, res, true, phone.accesstoken)
            .then(function (resultvalue) {
                sql = 'SELECT * FROM user where account=?';
                addSqlParams = [phone.user_account];

                client.query(sql, addSqlParams, function (err, result) {
                    if (err) {
                        publictool.returnErr(res, '查询参数错误:' + '[SELECT ERROR] - ' + err.message, 1);
                        client.end();
                        return;
                    }

                    if (result.length == 0) {
                        publictool.returnErr(res, '未找到该用户:' + phone.user_account, 6);
                        client.end();
                        return;
                    }

                    var modSql = 'UPDATE user SET end_timestamp = ? WHERE account = ?';
                    var modSqlParams = [phone.end_timestamp, phone.user_account];
                    client.query(modSql, modSqlParams, function (err, result) {
                        if (err) {
                            publictool.returnErr(res, '更新参数错误:' + '[UPDATE ERROR] - ' + err.message, 1);
                            client.end();
                            return;
                        }
                        publictool.returnErr(res, '操作成功', 0);
                        client.end();
                        return;
                    });
                });
            });
    });
};